package com.zuoqi.easywork.userManage.service;

import com.zuoqi.common.utils.IMap;
import com.zuoqi.easywork.system.service.OpLogService;
import com.zuoqi.easywork.userManage.mapper.TcUserScheduleMapper;
import com.zuoqi.easywork.userManage.model.TcUserSchedule;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ScheduleService {
    @Autowired
    private TcUserScheduleMapper tcUserScheduleMapper;
    @Autowired
    private OpLogService opLogService;
    /**
     * 获取部员所在项目列表
     * */
    public List<IMap>getProjectListForUser(IMap param){
        String todayStart = DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00");
        String todayEnd = DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:59");
        param.put("todayStart",todayStart);
        param.put("todayEnd",todayEnd);
        return tcUserScheduleMapper.getProjectListForUser(param);
    }

    /**
     * 进度汇报列表
     * */
    public List<IMap> getScheduleList(IMap params){
        return tcUserScheduleMapper.getScheduleList(params);
    }

    /**
     * 提交汇报内容
     * */
    public int addSchedule(IMap param){
        String proId = param.getString("proId");
        String schDescribe = param.getString("schDescribe");
        String updateUser = param.getString("updateUser");
        //验重
        String todayStart = DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00");
        String todayEnd = DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:59");
        IMap iMap = new IMap();
        iMap.put("projectId",proId);
        iMap.put("userId",updateUser);
        iMap.put("todayStart",todayStart);
        iMap.put("todayEnd",todayEnd);
        IMap ret = tcUserScheduleMapper.checkData(iMap);
        if (ret!=null)return 0;
        //创建进度报告实体
        TcUserSchedule tcUserSchedule = new TcUserSchedule();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        tcUserSchedule.setSchId("s"+datetime+updateUser);
        tcUserSchedule.setUserId(updateUser);
        tcUserSchedule.setUserName(param.getString("userName"));
        tcUserSchedule.setProId(proId);
        tcUserSchedule.setSchDescribe(schDescribe);
        tcUserSchedule.setSchDate(new Date());
        tcUserSchedule.setState("1");
        try {
            tcUserScheduleMapper.insert(tcUserSchedule);
            opLogService.addLog(param.getString("updateUser"),"提交项目进度汇报","项目ID："+proId);
        }catch (Exception e){
            return 9999;
        }
        return 1;
    }
    /**
     * 撤销汇报记录
     * */
    public int recallSchedule(IMap param){
        String schId = param.getString("schId");
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
        String datetime = tempDate.format(new Date());
        String schIdCode = schId.substring(1,9);
        if (!datetime.equals(schIdCode))return 0;
        TcUserSchedule tcUserSchedule = new TcUserSchedule();
        tcUserSchedule.setSchId(schId);
        tcUserSchedule.setState("0");
        try {
            tcUserScheduleMapper.updateByPrimaryKeySelective(tcUserSchedule);
            opLogService.addLog(param.getString("updateUser"),"注销汇报信息","记录ID："+schId);
        }catch (Exception e){
            return 9999;
        }
        return 1;
    }
}

